﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CanvasTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Navi.selectMode = DrawType.None;
        }
        public int Count = 0;
        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //Module module = new Module();
            //module.Width = 100;
            //module.Height = 80;
            //var target = e.GetPosition(canvas);
           
            //canvas.Children.Add(module);
            //canvas.RegisterName($"md{Count++}", module);
            //module.SetValue(Canvas.TopProperty, target.Y - 40);
            //module.SetValue(Canvas.LeftProperty, target.X - 50);
        }

        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Thumb myThumb = (Thumb)sender; 
            double nTop = Canvas.GetTop(myThumb) + e.VerticalChange;
            double nLeft = Canvas.GetLeft(myThumb) + e.HorizontalChange;
            Canvas.SetTop(myThumb, nTop);
            Canvas.SetLeft(myThumb, nLeft);
            

        }
        private void UpdateThumb()
        {
            //var point1 = new Point((double)thumb1.GetValue(Canvas.LeftProperty) + thumb1.Width, (double)thumb1.GetValue(Canvas.TopProperty) + thumb1.Height / 2);
            //var point2 = new Point((double)thumb2.GetValue(Canvas.LeftProperty), (double)thumb2.GetValue(Canvas.TopProperty) + thumb2.Height / 2);
            //path.SetValue(Canvas.LeftProperty, Math.Min(point1.X, point2.X));
            //path.SetValue(Canvas.TopProperty, Math.Min(point1.Y, point2.Y));
            //arrow.SetValue(Canvas.LeftProperty, point2.X );
            //arrow.SetValue(Canvas.TopProperty, point2.Y);


            //path.Width = Math.Abs(point1.X - point2.X);
            //path.Height = Math.Abs(point1.Y - point2.Y);
            //if (point1.X < point2.X)
            //{
            //    scale.ScaleX = point1.Y < point2.Y ? 1 : -1;
            //}
            //else
            //{
            //    scale.ScaleX = point1.Y < point2.Y ? -1 : 1;


            //}

        }
        bool isclicked = false;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(!isclicked) 
            {
                DoubleAnimation daX = new DoubleAnimation();
                DoubleAnimation daY = new DoubleAnimation();
                //指定起点
                daX.From = -100;
                daY.From = 0;
                //指定终点

                daX.To = 0;
                daY.To = 0;
                //指定时长
                Duration duration = new Duration(TimeSpan.FromMilliseconds(300));
                daX.Duration = duration;
                daY.Duration = duration;
                //动画的主题是TranslateTransform变形，而非Button
                this.Navi.RenderTransform.BeginAnimation(TranslateTransform.XProperty, daX);
                this.Navi.RenderTransform.BeginAnimation(TranslateTransform.YProperty, daY);
                this.navi_btn.RenderTransform.BeginAnimation(TranslateTransform.XProperty, daX);
                this.canvas.RenderTransform.BeginAnimation(TranslateTransform.XProperty, daX);
                isclicked = true;
            }
            else
            {
                DoubleAnimation daX = new DoubleAnimation();
                DoubleAnimation daY = new DoubleAnimation();
                //指定起点
                daX.From = 0;
                daY.From = 0;
                //指定终点

                daX.To = -100;
                daY.To = 0;
                //指定时长
                Duration duration = new Duration(TimeSpan.FromMilliseconds(300));
                daX.Duration = duration;
                daY.Duration = duration;
                //动画的主题是TranslateTransform变形，而非Button
                this.Navi.RenderTransform.BeginAnimation(TranslateTransform.XProperty, daX);
                this.Navi.RenderTransform.BeginAnimation(TranslateTransform.YProperty, daY);
                this.navi_btn.RenderTransform.BeginAnimation(TranslateTransform.XProperty, daX);
                this.canvas.RenderTransform.BeginAnimation(TranslateTransform.XProperty, daX);
                isclicked = false;
            }
            

        }
        List<List<Thumb>> thumbs = new List<List<Thumb>>();
        private void canvas_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            
            var point = e.GetPosition(canvas);
            switch(Navi.selectMode)
            {
                case DrawType.大IO:
                    
                    Thumb thumb3 = new Thumb();
                    thumb3.Style = this.FindResource("ThumbStyle1") as Style;
                    thumb3.Width = 80;
                    thumb3.Height = 80;
                    thumb3.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF808080"));
                    canvas.Children.Add(thumb3);
                    thumb3.SetValue(Canvas.LeftProperty, point.X -40);
                    thumb3.SetValue(Canvas.TopProperty, point.Y - 40);
                    thumb3.DragDelta += Thumb_DragDelta;
                    break;
                case DrawType.流线右:
                    Thumb thumbright = new Thumb();
                    thumbright.Style = this.FindResource("ThumbStyle_Line") as Style;
                    thumbright.Width = 80;
                    thumbright.Height = 80;
                    canvas.Children.Add(thumbright);
                    thumbright.SetValue(Canvas.LeftProperty, point.X-40 );
                    thumbright.SetValue(Canvas.TopProperty, point.Y-40 );
                    thumbright.DragDelta += Thumb_DragDelta;

                    Thumb thumbDragRight = new Thumb();
                    thumbDragRight.Width = 10;
                    thumbDragRight.Height = 10;
                    canvas.Children.Add(thumbDragRight);
                    thumbDragRight.SetValue(Canvas.LeftProperty, point.X+40 );
                    thumbDragRight.SetValue(Canvas.TopProperty, point.Y);
                    thumbDragRight.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF20ff80"));
                    thumbDragRight.DragDelta += Thumbright_DragDelta;

                    Thumb thumbDragLeft = new Thumb();
                    thumbDragLeft.Width = 10;
                    thumbDragLeft.Height = 10;
                    canvas.Children.Add(thumbDragLeft);
                    thumbDragLeft.SetValue(Canvas.LeftProperty, point.X - 50);
                    thumbDragLeft.SetValue(Canvas.TopProperty, point.Y);
                    thumbDragLeft.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF20ff80"));
                    thumbDragLeft.DragDelta += ThumbDragLeft_DragDelta;
                    thumbs.Add(new List<Thumb>()
                    {
                        thumbDragLeft,thumbright,thumbDragRight
                    });
                    //thumbright.DragDelta += Thumbright_DragDelta;
                    break;
            }
            //Navi.selectMode = DrawType.None;
        }

        private void ThumbDragLeft_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb myThumb = (Thumb)sender;
            
            double nLeft = Canvas.GetLeft(myThumb) + e.HorizontalChange;
          
            Canvas.SetLeft(myThumb, nLeft);

        }

        private void Thumbright_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb myThumb = (Thumb)sender;
            var thumb = thumbs.Where(t => t[2] == myThumb).First().ToList();
            
            double nLeft = Canvas.GetLeft(myThumb) + e.HorizontalChange;
            
            Canvas.SetLeft(myThumb, nLeft);
            thumb[1].Width += e.HorizontalChange;

        }

        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            
        }
    }
}
